"""
通讯录page
"""
from selenium.webdriver.common.by import By

from we_com.pages.base_page import BasePage


class ContactsPage(BasePage):
    _url = 'https://work.weixin.qq.com/wework_admin/frame#/contacts'
    __add_department_outer = (By.CSS_SELECTOR, '.member_colLeft_top_addBtn')
    __add_department_inner = (By.CSS_SELECTOR, '.js_create_party')
    __department_name = (By.CSS_SELECTOR, '[name="name"]')
    __add_member_1 = (By.CSS_SELECTOR, '.js_has_member>div:first-child>a:nth-child(2)')
    __add_member_2 = (By.CSS_SELECTOR, '.js_has_member>div:last-child>a:nth-child(2)')
    __select_department = (By.CSS_SELECTOR, '.js_toggle_party_list')
    __top_department = (By.CSS_SELECTOR, '.form>div:last-child>div>div>ul>li>a')
    __department_a_names = (By.CSS_SELECTOR, '.form>div:last-child a')
    __add_department_confirm = (By.CSS_SELECTOR, '.ww_btn_Blue')
    __add_department_cancel = (By.CSS_SELECTOR, '.ww_commonImg_CloseDialog')
    __username = (By.CSS_SELECTOR, '#username')
    __account = (By.CSS_SELECTOR, '#memberAdd_acctid')
    __email = (By.CSS_SELECTOR, '#memberAdd_biz_mail')
    __phone = (By.CSS_SELECTOR, '#memberAdd_phone')
    __aliss = (By.CSS_SELECTOR, '#memberAdd_english_name')
    __save_button = (By.CSS_SELECTOR, '.js_btn_continue')
    __err_msg = (By.CSS_SELECTOR, '.ww_inputWithTips_tips')
    __form = (By.CSS_SELECTOR, '.form')
    __result_msg = (By.CSS_SELECTOR, '#js_tips')

    def add_department(self, department_name, department_super_name=None):
        self.click_then_presence(self.__add_department_outer)
        self.click_then_presence(self.__add_department_inner)
        self.presence(self.__department_name).send_keys(department_name)
        self.sleep(0.3)
        self.click_clickalbe(self.__select_department)
        eles = self.driver.find_elements(*self.__department_a_names)
        for ele in eles:
            text = self.get_text(ele)
            if text == department_super_name:
                self.click_clickalbe(ele)
                break

        # self.click_clickalbe(self.__top_department)  # 偷懒写法 , 不循环查找父级部门 , 直接在顶级部门下新建部门 .
        # self.find(self.__form).submit()  # 虽然是 form , 但是 submit() 并不管用 , 好像是禁用了 submit()
        # self.click_and_presence(self.__add_department_confirm)  # 元素不可点击 , 等待可点击后点击校验通过 , 依旧不可点击 ...
        self.move_to_and_enter(self.__add_department_confirm)  # 简单办法是使用 Enter 代替点击 . 此处可行 .
        return self.get_text(self.__result_msg)

    def add_member(self, username, account, email, phone):
        self.input_visibility(self.__username, username)
        self.input_visibility(self.__account, account)
        self.input_visibility(self.__email, email)
        self.input_visibility(self.__phone, phone)
        self.click_presence(self.__save_button)
        # self.get_text(self.__result_msg)
        self.click(self.__aliss)
        return self

    def get_add_member_success_msg(self):
        return self.get_text(self.__result_msg)

    def get_add_member_error_msg(self):
        ele_s = self.find_s(self.__err_msg)
        err_msg = [self.get_text(ele) for ele in ele_s if self.get_text(ele)]
        return err_msg
